//
//  DUDetailViewController.m
//  toutiao
//
//  Created by duyujin1 on 2021/7/22.
//

#import "DUDetailViewController.h"
#import <WebKit/WebKit.h>
#import "DUMediator.h"
#import "DULogin.h"
#import "define.h"

@interface DUDetailViewController ()<WKNavigationDelegate>

@property(nonatomic,strong,readwrite) WKWebView * webView;
@property(nonatomic,strong,readwrite) UIProgressView * progressView;
@property(nonatomic,strong,readwrite) NSString * articleUrl;

@end

@implementation DUDetailViewController
+(void)load{
//    [DUMediator registerScheme:@"detail://" processBlock:^(NSDictionary * _Nonnull pramas) {
//        NSString * url = (NSString *)[pramas objectForKey:@"url"];
//        UINavigationController * navigationController=(UINavigationController *)[pramas objectForKey:@"controller"];
//        DUDetailViewController * controller=[[DUDetailViewController alloc] initWithUrlString:url];
//        [navigationController pushViewController:controller animated:YES];
//    }];
    [DUMediator registerProtocol:@protocol(DUDetailViewControllerProtocol) class:[self class]];
}

-(void)dealloc{
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

-(instancetype)initWithUrlString:(NSString *)urlString{
    if(self=[super init]){
        self.articleUrl=urlString;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=UIColor.whiteColor;
    if([self.articleUrl isEqualToString:@""]){
      UIImageView * notFoundView=[[UIImageView alloc] initWithFrame:self.view.bounds];
      notFoundView.image=[UIImage imageNamed:@"img-404"];
      notFoundView.contentMode=UIViewContentModeScaleAspectFit;
      [self.view addSubview:notFoundView];
      return;
    }
    
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(_shareArticle)];
    UIImage * shareImage=[UIImage imageNamed:@"share"];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:shareImage style:UIBarButtonItemStylePlain target:self action:@selector(_shareArticle)];
    
    [self.view addSubview:({
        self.webView=[[WKWebView alloc] initWithFrame:CGRectMake(0, NavAndStatusHeight , self.view.frame.size.width, self.view.frame.size.height-NavAndStatusHeight)];
        self.webView.navigationDelegate=self;
        self.webView;
    })];
    
    [self.view addSubview:({
        self.progressView=[[UIProgressView alloc] initWithFrame:CGRectMake(0, NavAndStatusHeight, self.view.frame.size.width, 20)];
        self.progressView;
    })];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.articleUrl]]];
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    decisionHandler(WKNavigationActionPolicyAllow);
}
    
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    self.progressView.progress=self.webView.estimatedProgress;
    NSLog(@"keyPath changed");
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    NSLog(@"didFinish");
    self.progressView.hidden=YES;
}

-(__kindof UIViewController *)detailViewControllerWithUrl:(NSString *)detailUrl{
    return [[[self class] alloc] initWithUrlString:detailUrl];
}

-(void)_shareArticle{
    [[DULogin sharedLogin] shareToQQWithArticleUrl:[NSURL URLWithString:self.articleUrl] formViewController:self];
}

-(void)dismiss:(UIAlertController *)alert{
    [alert dismissViewControllerAnimated:YES completion:nil];
}

+(void)dismiss:(UIAlertController *)alert afterDelay:(NSTimeInterval)delayTime formViewController:(UIViewController *)vc{
    [vc presentViewController:alert animated:YES completion:nil];
    [vc performSelector:@selector(dismiss:) withObject:alert afterDelay:delayTime];
}
@end
